package cn.hongxin.sap.conn.utils;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;

import java.util.HashMap;
import java.util.Properties;

public class CustomSAPDestinationDataProvider {
    static class MyDestinationDataProvider implements DestinationDataProvider {
    private DestinationDataEventListener eL;
    private HashMap<String, Properties> destinations;
    private static MyDestinationDataProvider provider = new MyDestinationDataProvider();
    private MyDestinationDataProvider() {// 单例模式
        if (provider == null) {
            destinations = new HashMap<String, Properties>();
        }
    }
    public static MyDestinationDataProvider getInstance() {
        return provider;
    }
    // 实现接口：获取连接配置属性
    public Properties getDestinationProperties(String destinationName) {
        if (destinations.containsKey(destinationName)) {
            return destinations.get(destinationName);
        } else {
            return null;
        }
    }
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
        this.eL = eventListener;
    }
    public boolean supportsEvents() {
        return false;
    }
    /**
     * Add new destination 添加连接配置属性
     *
     * @param properties
     *            holds all the required data for a destination
     **/
    void addDestination(String destinationName, Properties properties) {
        synchronized (destinations) {
            destinations.put(destinationName, properties);
        }
    }

    //implementation that saves the properties in a very secure way
    void changeProperties(String destName, Properties properties){
        synchronized(destinations){
            if(properties==null){
                if(destinations.remove(destName)!=null)
                    eL.deleted(destName);
            }
            else{
                destinations.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }

}
    public static void main(String[] args) throws Exception {
        // 获取单例
        MyDestinationDataProvider myProvider = MyDestinationDataProvider
                .getInstance();
        // Register the MyDestinationDataProvider 环境注册
        Environment.registerDestinationDataProvider(myProvider);
        // TEST 01：直接测试
        // ABAP_AS is the test destination name ：ABAP_AS为目标连接属性名（只是逻辑上的命名）
        String destinationName = "ABAP_AS";
        System.out.println("Test destination - " + destinationName);
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,
                "172.16.0.86");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
        connectProperties
                .setProperty(DestinationDataProvider.JCO_CLIENT, "800");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,
                "srm");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,
                "SRM@1234");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh");
        // Add a destination
        myProvider.addDestination(destinationName, connectProperties);
        // Get a destination with the name of "ABAP_AS"
        JCoDestination DES_ABAP_AS = JCoDestinationManager
                .getDestination(destinationName);
        // Test the destination with the name of "ABAP_AS"
        try {
            DES_ABAP_AS.ping();
            System.out.println("Destination - " + destinationName + " is ok");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Destination - " + destinationName
                    + " is invalid");
        }
    }
}
